package com.huluobo.a2308douyindemo.utils

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.huluobo.a2308douyindemo.R

/**
 *  Created by LC on 2025/10/23.
 */
object ImageUtils {
    //加载普通图片
    fun loadImage(context: Context, url: String, imageView: ImageView) {
        Glide.with(context)
            .load(url)
            //占位图
            .placeholder(ColorDrawable(Color.GRAY))
            //加载错误时
            .error(R.drawable.baseline_error_24)
            //跳过内存缓存
            .skipMemoryCache(true)
            //全局使用磁盘缓存
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            //裁剪规则,从图片中心开始拉伸裁剪
            .transform(CenterCrop())
            .into(imageView)
    }

    //加载圆形图片
    fun loadCircleImage(context: Context, url: String, imageView: ImageView) {
        Glide.with(context)
            .load(url)
            //占位图
            .placeholder(ColorDrawable(Color.GRAY))
            //加载错误时
            .error(R.drawable.baseline_error_24)
            //跳过内存缓存
            .skipMemoryCache(true)
            //全局使用磁盘缓存
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            //裁剪规则,从图片中心开始拉伸裁剪,圆形裁剪
            .transform(CenterCrop(), CircleCrop())
            .into(imageView)
    }

    fun loadRoundImage(context: Context, url: String, imageView: ImageView, r: Int) {
        Glide.with(context)
            .load(url)
            //占位图
            .placeholder(ColorDrawable(Color.GRAY))
            //加载错误时
            .error(R.drawable.baseline_error_24)
            //跳过内存缓存
            .skipMemoryCache(true)
            //全局使用磁盘缓存
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            //裁剪规则,从图片中心开始拉伸裁剪,圆角裁剪,传入参数r半径
            .transform(CenterCrop(), RoundedCorners(r))
            .into(imageView)
    }
}